<!DOCTYPE html>
<!-- Ported from the OpenGL Samples Pack https://github.com/g-truc/ogl-samples/blob/master/tests/gl-330-sampler-object.cpp -->
<html lang="en">

<head>
    <title>WebGL 2 Samples - sampler_object</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="utility.js"></script>
</head>

<body>
    <div id="info">WebGL 2 Samples - sampler_object</div>
    <p id="description">
    This sample demonstrates using 2 sampler objects for texture filtering.
    <br>
    BOTTOM: Linear filtering / TOP: Nearest filtering
    </p>


    <script id="vs" type="x-shader/x-vertex">
        #version 300 es
        #define POSITION_LOCATION 0
        #define TEXCOORD_LOCATION 4

        precision highp float;
        precision highp int;

        uniform mat4 mvp;

        layout(location = POSITION_LOCATION) in vec2 position;
        layout(location = TEXCOORD_LOCATION) in vec2 textureCoordinates;

        out vec2 v_st;

        void main()
        {
            v_st = textureCoordinates;
            gl_Position = mvp * vec4(position, 0.0, 1.0) ;
        }
    </script>

    <script id="fs" type="x-shader/x-fragment">
        #version 300 es
        #define FRAG_COLOR_LOCATION 0

        precision highp float;
        precision highp int;

        struct Material
        {
            sampler2D diffuse[2];
        };

        uniform Material material;

        in vec2 v_st;

        layout(location = FRAG_COLOR_LOCATION) out vec4 color;

        void main()
        {
            if (v_st.y / v_st.x < 1.0) {
                color = texture(material.diffuse[0], v_st);
            } else {
                color = texture(material.diffuse[1], v_st) * 0.77;
            }
        }
    </script>

    <script>
    (function() {
        'use strict';

        var canvas = document.createElement('canvas');
        canvas.width = Math.min(window.innerWidth, window.innerHeight);
        canvas.height = canvas.width;
        document.body.appendChild(canvas);

        var gl = canvas.getContext( 'webgl2', { antialias: false } );
        var isWebGL2 = !!gl;
        if(!isWebGL2) {
            document.getElementById('info').innerHTML = 'WebGL 2 is not available.  See <a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">How to get a WebGL 2 implementation</a>';
            return;
        }

        // -- Initialize program

        var program = createProgram(gl, getShaderSource('vs'), getShaderSource('fs'));

        var uniformMvpLocation = gl.getUniformLocation(program,'mvp');
        var uniformDiffuse0Location = gl.getUniformLocation(program,'material.diffuse[0]');
        var uniformDiffuse1Location = gl.getUniformLocation(program, 'material.diffuse[1]');

        // -- Initialize buffer

        var positions = new Float32Array([
            -1.0, -1.0,
             1.0, -1.0,
             1.0,  1.0,
             1.0,  1.0,
            -1.0,  1.0,
            -1.0, -1.0
        ]);
        var vertexPosBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        var texcoords = new Float32Array([
            0.0, 1.0,
            1.0, 1.0,
            1.0, 0.0,
            1.0, 0.0,
            0.0, 0.0,
            0.0, 1.0
        ]);
        var vertexTexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, texcoords, gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER,null);

        // -- Initialize vertex array

        var vertexArray = gl.createVertexArray();
        gl.bindVertexArray(vertexArray);

        var vertexPosLocation = 0; // set with GLSL layout qualifier
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
        gl.vertexAttribPointer(vertexPosLocation, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(vertexPosLocation);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        var vertexTexLocation = 4; // set with GLSL layout qualifier
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
        gl.vertexAttribPointer(vertexTexLocation, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(vertexTexLocation);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        gl.bindVertexArray(null);

        // -- Initialize samplers

        var samplerA = gl.createSampler();

        gl.samplerParameteri(samplerA, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
        gl.samplerParameteri(samplerA, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.samplerParameteri(samplerA, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.samplerParameteri(samplerA, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.samplerParameteri(samplerA, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
        gl.samplerParameterf(samplerA, gl.TEXTURE_MIN_LOD, -1000.0);
        gl.samplerParameterf(samplerA, gl.TEXTURE_MAX_LOD, 1000.0);
        gl.samplerParameteri(samplerA, gl.TEXTURE_COMPARE_MODE, gl.NONE);
        gl.samplerParameteri(samplerA, gl.TEXTURE_COMPARE_FUNC, gl.LEQUAL);

        var samplerB = gl.createSampler();
        gl.samplerParameteri(samplerB, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
        gl.samplerParameteri(samplerB, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
        gl.samplerParameteri(samplerB, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.samplerParameteri(samplerB, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.samplerParameteri(samplerB, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
        gl.samplerParameterf(samplerB, gl.TEXTURE_MIN_LOD, -1000.0);
        gl.samplerParameterf(samplerB, gl.TEXTURE_MAX_LOD, 1000.0);
        gl.samplerParameteri(samplerB, gl.TEXTURE_COMPARE_MODE, gl.NONE);
        gl.samplerParameteri(samplerB, gl.TEXTURE_COMPARE_FUNC, gl.LEQUAL);

        // -- Load texture then render

        var imageUrl = '../assets/img/Di-3d.png';
        var texture;
        loadImage(imageUrl, function(image) {
            texture = gl.createTexture();
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.texImage2D(
                gl.TEXTURE_2D,
                0, // Level of details
                gl.RGBA, // Format
                gl.RGBA,
                gl.UNSIGNED_BYTE, // Size of each channel
                image
            );
            gl.generateMipmap(gl.TEXTURE_2D);

            render();
        });

       function render() {
            // Clear color buffer
            gl.clearColor(0.0, 0.0, 0.0, 1.0);
            gl.clear(gl.COLOR_BUFFER_BIT);

            // Bind program
            gl.useProgram(program);

            var matrix = new Float32Array([
                0.8, 0.0, 0.0, 0.0,
                0.0, 0.8, 0.0, 0.0,
                0.0, 0.0, 0.8, 0.0,
                0.0, 0.0, 0.0, 1.0
            ]);
            gl.uniformMatrix4fv(uniformMvpLocation, false, matrix);
            gl.uniform1i(uniformDiffuse0Location, 0);
            gl.uniform1i(uniformDiffuse1Location, 1);

            gl.bindVertexArray(vertexArray);

            gl.activeTexture(gl.TEXTURE0);
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.bindSampler(0, samplerA);

            gl.activeTexture(gl.TEXTURE1);
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.bindSampler(1, samplerB);

            gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, 1);

            // Cleanup
            gl.deleteBuffer(vertexPosBuffer);
            gl.deleteBuffer(vertexTexBuffer);
            gl.deleteSampler(samplerA);
            gl.deleteSampler(samplerB);
            gl.deleteVertexArray(vertexArray);
            gl.deleteTexture(texture);
            gl.deleteProgram(program);
        }
    })();
    </script>
    <div id="highlightedLines"  style="display: none">#L145-L166</div>

</body>

</html>
